home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / ulib / n2a.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  136 lines

  1. /***********************************************************************
  2.  * $Id: n2a.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
  3.  *
  4.  *.  Copyright(c) 1993,1994 by T.C. Zhao
  5.  *   All rights reserved.
  6.  *.
  7.  * Convert numbers to strings. Not the fastest but faster than sprintf
  8.  * Converted strings are in a static buffer.
  9.  *
  10.  * On IRIX 3.3, itoa is about 3 times faster than sprintf
  11.  *              ftoa is about 4 times (6digits)faster than sprintf
  12.  ***********************************************************************/
  13. #if !defined(lint) && defined(F_ID)
  14. char *id_n2a = "$Id: n2a.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
  15. #endif
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "ulib.h"
  20.  
  21. static const char *digits = "0123456789";
  22.  
  23. /************* convert integer to string **********/
  24.  
  25. const char *
  26. itoa(register int n)
  27. {
  28.     static char buf[20];
  29.     register char *p = buf + sizeof(buf);
  30.     register int l, sign = 0;
  31.  
  32.     if (n < 0)
  33.       {
  34.       sign = '-';
  35.       n = -n;        /* might overflow if n most negative */
  36.       }
  37.  
  38.     *--p = '\0';        /* terminating 0 */
  39.     while (n >= 10)
  40.       {
  41.       *--p = *(digits + (n - (l = n / 10) * 10));
  42.       n = l;
  43.       }
  44.     *--p = *(digits + n);
  45.     if (sign)
  46.     *--p = sign;
  47.     return p;
  48. }
  49.  
  50. /*******  convert float to a string. Max keep 6 digits *****/
  51.  
  52. static int multab[] =
  53. {
  54.     1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
  55. };
  56.  
  57. const char *
  58. ftoa(float f, int n)
  59. {
  60.     static char buf[33];
  61.     register char *fbuf;
  62.     static int nmax = sizeof(multab) / sizeof(multab[0]);
  63.     int i, sign = 0;
  64.     register float ff;
  65.     register int fi, ffi, l;
  66.  
  67. #if 1
  68.     if (n >= nmax || n < 0)
  69.     n = nmax - 1;
  70.  
  71.     if (f < 0)
  72.       {
  73.       f = -f;
  74.       sign = '-';
  75.       }
  76.  
  77.     f += 0.05 / multab[n];    /* rounding */
  78.     fi = f;
  79.     ff = f - fi;
  80.  
  81.     *(fbuf = buf + n + 2) = '.';
  82.  
  83.     if (ff < 1.0 / multab[n])
  84.       {
  85.       *++fbuf = '0';
  86.       }
  87.     else
  88.       {
  89.       for (i = 1; i <= n; i++)
  90.         {
  91.         *++fbuf = *(digits + (ffi = (ff *= 10.0)));
  92.         ff = ff - ffi;
  93.         }
  94.       }
  95.     *++fbuf = '\0';
  96.     /* covert the interal part */
  97.  
  98.     fbuf = buf + n + 2;
  99.     while (fi >= 10)
  100.       {
  101.       *--fbuf = *(digits + (fi - (l = fi / 10) * 10));
  102.       fi = l;
  103.       }
  104.     *--fbuf = *(digits + fi);
  105.     if (sign)
  106.     *--fbuf = sign;
  107.     return fbuf;
  108.  
  109. #else
  110.     sprintf(buf, "%.*f", n, f);
  111.     return buf;
  112. #endif
  113. }
  114.  
  115.  
  116. #ifdef TEST
  117. #include <stdio.h>
  118. main()
  119. {
  120.     static int nn[] =
  121.     {0, 1, 5, -1, -10, 1234567, -121};
  122.     static float ff[] =
  123.     {1.2, -2.1, 0.01, 0.001, 1.00, -123456.252, 1.23552};
  124.     int j;
  125.  
  126.     for (j = 0; j < sizeof(nn) / sizeof(nn[0]); j++)
  127.     fprintf(stderr, "itoa(%d)=%s\n", nn[j], itoa(nn[j]));
  128.  
  129.     for (j = 0; j < sizeof(ff) / sizeof(ff[0]); j++)
  130.     fprintf(stderr, "ftoa(%f, %d)=%s\n", ff[j], j, ftoa(ff[j], j));
  131.  
  132.  
  133. }
  134.  
  135. #endif
  136.